using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Devsoft.Utilities
{
    public class XmlHelper
    {
        /// <summary>
        /// Set value for a xml node
        /// </summary>
        /// <param name="xmlDoc">The document owned the node be wanted to set text value</param>
        /// <param name="strPath">XPath to select the node</param>
        /// <param name="strValue">Value be wanted to set</param>
        public static void SetNodeText(XmlDocument xmlDoc, string strPath, string strValue)
        {
            XmlNode xNode = xmlDoc.SelectSingleNode(strPath);
            if (xNode != null)
            {
                xNode.InnerText = strValue;
            }//End If
        }
        /// <summary>
        /// Get text of a xml Node
        /// </summary>
        /// <param name="xmlDoc">The document owned the node be wanted to set text value</param>
        /// <param name="strPath">XPath to select the node</param>
        /// <returns>value of the specified node</returns>
        public static string GetNodeText(XmlDocument xmlDoc, string strPath)
        {
            XmlNode xNode = xmlDoc.SelectSingleNode(strPath);
            if (xNode != null)
            {
                return xNode.InnerText;
            }//End If
            return null;
        }

        /// <summary>
        /// Ensures the specified xml node existing.
        /// </summary>
        /// <param name="xmlDoc">The XML document.</param>
        /// <param name="strPath">The xpath.</param>
        /// <returns></returns>
        public static XmlNode EnsureNodeExisting(XmlDocument xmlDoc,string strPath)       
        {
            XmlNode xmlNode = xmlDoc.SelectSingleNode(strPath);
            if (xmlNode == null)
            {
                string[] arrNode = strPath.Split('/');

                if (arrNode.Length < 1)
                {
                    throw new Exception("The xpath is invalid syntax");
                }//End If
                
                int n = arrNode.Length;
                int k = 0;
                for (int i = n - 1; i > 0 ; i--)
                {
                    string strTemp = string.Join("/",arrNode,0,i + 1);
                    xmlNode = xmlDoc.SelectSingleNode(strTemp);
                	if (xmlNode != null)
                	{
                        k = i;
                        break;
                	}//End If
                }//End For

                XmlNode xmlTemp;
                if (k == 0)
                {
                    //No root element ? just create a new one
                    xmlTemp = xmlDoc.CreateElement(arrNode[1]);
                    xmlDoc.AppendChild(xmlTemp);
                    xmlNode = xmlTemp;
                    k = 1;
                }//End If

                k++;

                
                for (int i = k; i < n; i++)
                {
                    xmlTemp = xmlDoc.CreateElement(arrNode[i]);
                    xmlNode.AppendChild(xmlTemp);
                    xmlNode = xmlTemp;
                }//End For
                
                
            }//End If
            return xmlNode;
        }

    }
}
